home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / shwdib.zip / PRINT.C < prev    next >
C/C++ Source or Header  |  1993-07-26  |  8KB  |  189 lines

  1. /*******************************************************************************
  2.  *                                                                             *
  3.  *  MODULE      : Print.c                                                      *
  4.  *                                                                             *
  5.  *  DESCRIPTION : Routines used for printing.                                  *
  6.  *                                                                             *
  7.  *  FUNCTIONS   : GetPrinterDC()   - Gets default printer from WIN.INI and     *
  8.  *                                   creates a DC for it.                      *
  9.  *                                                                             *
  10.  *                InitPrinting()   - Initializes print job.                    *
  11.  *                                                                             *
  12.  *                TermPrinting()   - Terminates print job.                     *
  13.  *                                                                             *
  14.  *                PrintDlgProc()   - Dialog function for the "Cancel Printing" *
  15.  *                                   dialog.                                   *
  16.  *                                                                             *
  17.  *                AbortProc()      - Peeks at message queue for messages from  *
  18.  *                                   the print dialog.                         *
  19.  *                                                                             *
  20.  *******************************************************************************/
  21. // COPYRIGHT:
  22. //
  23. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  24. //
  25. //   You have a royalty-free right to use, modify, reproduce and
  26. //   distribute the Sample Files (and/or any modified version) in
  27. //   any way you find useful, provided that you agree that
  28. //   Microsoft has no warranty obligations or liability for any
  29. //   Sample Application Files which are modified.
  30. #include <windows.h>
  31. #include <string.h>
  32. #include "showdib.h"
  33.  
  34. FARPROC lpfnAbortProc = NULL;
  35.  
  36. FARPROC lpfnPrintDlgProc = NULL;
  37.  
  38. HWND hWndParent = NULL;
  39.  
  40. HWND hDlgPrint = NULL;
  41.  
  42. BOOL bError;
  43.  
  44. BOOL bUserAbort;
  45.  
  46.  
  47. BOOL FAR PASCAL AbortProc (HDC, short);
  48. BOOL FAR PASCAL PrintDlgProc (HWND, unsigned, WORD, DWORD);
  49. #pragma alloc_text(_PRINT, AbortProc, PrintDlgProc)
  50.  
  51. /****************************************************************************
  52.  *                                                                          *
  53.  *  FUNCTION   : GetPrinterDC()                                             *
  54.  *                                                                          *
  55.  *  PURPOSE    : Read WIN.INI for default printer and create a DC for it.   *
  56.  *                                                                          *
  57.  *  RETURNS    : A handle to the DC if successful or NULL otherwise.        *
  58.  *                                                                          *
  59.  ****************************************************************************/
  60.  
  61.  
  62. HDC PASCAL GetPrinterDC ()
  63. {
  64.    static char szPrinter[80];
  65.    char *szDevice, *szDriver, *szOutput;
  66.  
  67.    GetProfileString("windows", "device", "", szPrinter, sizeof(szPrinter));
  68.    if ((szDevice = strtok(szPrinter, ",")) && (szDriver = strtok(NULL, ", "))
  69.        && (szOutput = strtok(NULL, ", ")))
  70.       return CreateDC(szDriver, szDevice, szOutput, NULL);
  71.    return NULL;
  72. }
  73. /****************************************************************************
  74.  *                                                                          *
  75.  *  FUNCTION   : InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)  *
  76.  *                                                                          *
  77.  *  PURPOSE    : Makes preliminary driver calls to set up print job.        *
  78.  *                                                                          *
  79.  *  RETURNS    : TRUE  - if successful.                                     *
  80.  *               FALSE - otherwise.                                         *
  81.  *                                                                          *
  82.  ****************************************************************************/
  83.  
  84.  
  85. BOOL PASCAL InitPrinting (HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)
  86. {
  87.    bError = FALSE;     /* no errors yet */
  88.    bUserAbort = FALSE;     /* user hasn't aborted */
  89.    hWndParent = hWnd;      /* save for Enable at Term time */
  90.    lpfnPrintDlgProc = MakeProcInstance(PrintDlgProc, hInst);
  91.    lpfnAbortProc = MakeProcInstance(AbortProc, hInst);
  92.    hDlgPrint = CreateDialog(hInst, "PRTDLG", hWndParent, lpfnPrintDlgProc);
  93.    if (!hDlgPrint)
  94.       return FALSE;
  95.    SetWindowText(hDlgPrint, msg);
  96.    EnableWindow(hWndParent, FALSE);        /* disable parent */
  97.    if ((Escape(hDC, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL) > 0) && (
  98.        Escape(hDC, STARTDOC, lstrlen(msg), msg, NULL) > 0))
  99.       bError = FALSE;
  100.    else
  101.       bError = TRUE;
  102.  
  103.    /* might want to call the abort proc here to allow the user to
  104.     * abort just before printing begins */
  105.    return TRUE;
  106. }
  107. /****************************************************************************
  108.  *                                                                          *
  109.  *  FUNCTION   :  TermPrinting(HDC hDC)                                     *
  110.  *                                                                          *
  111.  *  PURPOSE    :  Terminates print job.                                     *
  112.  *                                                                          *
  113.  ****************************************************************************/
  114.  
  115.  
  116. void PASCAL TermPrinting (HDC hDC)
  117. {
  118.    if (!bError)
  119.       Escape(hDC, ENDDOC, 0, NULL, NULL);
  120.    if (bUserAbort)
  121.       Escape(hDC, ABORTDOC, 0, NULL, NULL);
  122.    else
  123.    {
  124.       EnableWindow(hWndParent, TRUE);
  125.       DestroyWindow(hDlgPrint);
  126.    }
  127.    FreeProcInstance(lpfnAbortProc);
  128.    FreeProcInstance(lpfnPrintDlgProc);
  129. }
  130. /****************************************************************************
  131.  *                                                                          *
  132.  *  FUNCTION   :PrintDlgProc (HWND, unsigned , WORD , DWORD )               *
  133.  *                                                                          *
  134.  *  PURPOSE    :Dialog function for the "Cancel Printing" dialog. It sets   *
  135.  *              the abort flag if the user presses <Cancel>.                *
  136.  *                                                                          *
  137.  ****************************************************************************/
  138.  
  139.  
  140. BOOL FAR PASCAL PrintDlgProc (HWND hDlg, unsigned iMessage, WORD wParam, DWORD
  141.                               lParam)
  142. {
  143.    switch (iMessage)
  144.       {
  145.    case WM_INITDIALOG:
  146.       EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  147.       break;
  148.  
  149.    case WM_COMMAND:
  150.       bUserAbort = TRUE;
  151.       EnableWindow(hWndParent, TRUE);
  152.       DestroyWindow(hDlg);
  153.       hDlgPrint = 0;
  154.       break;
  155.  
  156.    default:
  157.       return FALSE;
  158.       }
  159.    return TRUE;
  160. }
  161.  
  162. /****************************************************************************
  163.  *                                                                          *
  164.  *  FUNCTION   :AbortProc (HDC hPrnDC, short nCode)                         *
  165.  *                                                                          *
  166.  *  PURPOSE    :Checks message queue for messages from the "Cancel Printing"*
  167.  *              dialog. If it sees a message, (this will be from a print    *
  168.  *              cancel command), it terminates.                             *
  169.  *                                                                          *
  170.  *  RETURNS    :Inverse of Abort flag                                       *
  171.  *                                                                          *
  172.  ****************************************************************************/
  173.  
  174.  
  175. BOOL FAR PASCAL AbortProc (HDC hPrnDC, short nCode)
  176. {
  177.    MSG msg;
  178.  
  179.    while (!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  180.    {
  181.       if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
  182.       {
  183.          TranslateMessage(&msg);
  184.          DispatchMessage(&msg);
  185.       }
  186.    }
  187.    return !bUserAbort;
  188. }
  189.